/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.JBTreeNode;
import com.jb.web.context.WebContext;

import java.util.ArrayList;
import java.util.Iterator;

import com.jb.faces.component.UIComponent;


public class CatalogRenderer extends MenuItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent item) {
        return renderBegin(pCtx, item).append(renderChildren(pCtx, item)).append(renderEnd(pCtx, item));
    }

    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
        JBTreeNode mi = (JBTreeNode) itemToRender;

        return new StringBuffer().append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" ")
                                 .append(addTagProperty("style", mi.getStyle(pCtx), null))
                                 .append(getEditScript(pCtx, mi)).append("><tbody>");
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent item) {
        JBTreeNode mi = (JBTreeNode) item;

        JBTreeNode selected = mi.getSelectedNode();

        if (selected == null) {
            selected = mi;
        }

        mi = (selected.getChildCount() == 0) ? selected.getParentNode() : selected;

        StringBuffer stringBuffer = new StringBuffer().append("<tr><td colspan=\"3\" nowrap=\"nowrap\">")
                                                      .append(this.renderPath(pCtx, mi)).append("</td></tr>");

        if ((mi != null) && (mi.getChildCount() > 0)) {
            for (Iterator i = mi.getChildNodes().iterator(); i.hasNext();) {
                stringBuffer.append("<tr><td width=\"4px\"").append(getEditScript(pCtx, mi)).append("></td><td align=\"center\" width=\"20px\">");

                JBTreeNode child = (JBTreeNode) i.next();

                if ((child.getIconImage() != null) && (child.getIconImage().length() > 0)) {
                    stringBuffer.append("<img src=\"").append(child.getIconImage()).append("\" />");
                } else {
                    stringBuffer.append("<img src=\"").append("resources/images/tree/page.gif").append("\" />");
                }

                stringBuffer.append("</td>").append(renderCell(pCtx, child)).append("</tr>");
            }
        }

        return stringBuffer;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        return new StringBuffer().append("</tbody></table>");
    }

    private StringBuffer renderCell(WebContext pCtx, JBTreeNode mi) {
        StringBuffer stringBuffer = new StringBuffer();
        String dest = (String) evaluate(pCtx, mi, mi.getDestination());
        dest = pCtx.formURL(dest, mi) + "";
        stringBuffer.append("<td ").append(addTagProperty("onClick", "JBUtil.gotoUrl('" + dest + "')", null))
                    .append(addTagProperty("width", "100%", null))
                    .append(addTagProperty("style", mi.getStyle(pCtx), null))
                    .append(addTagProperty("class", mi.isSelected() ? mi.getSelectedStyle(pCtx) : mi.getStyleClass(),
                null)).append(">");

        String label = (String) evaluate(pCtx, mi, mi.getValue(pCtx));

        if (isEmptyString(label)) {
            label = mi.getId();
        }

        stringBuffer.append(label);

        return stringBuffer.append("</td>");
    }

    private StringBuffer renderPath(WebContext pCtx, JBTreeNode mi) {
        StringBuffer stringBuffer = new StringBuffer();
        ArrayList path = new ArrayList();

        while (mi != null) {
            path.add(mi);
            mi = mi.getParentNode();
        }

        for (int i = path.size(); i > 0; i--) {
            mi = (JBTreeNode) path.get(i - 1);

            String label = (String) evaluate(pCtx, mi, mi.getValue(pCtx));

            if (isEmptyString(label)) {
                label = mi.getId();
            }

            String dest = (String) evaluate(pCtx, mi, mi.getDestination());
            dest = pCtx.formURL(dest, mi) + "";
            dest = "<a href=\"" + dest + "\">" + label + "</a>";
            stringBuffer.append(dest);

            if (i > 1) {
                stringBuffer.append("->");
            }
        }

        return stringBuffer;
    }
}
